package com.icbc.digitalwallet.common.config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


//@Configuration
public class WebConfig {

    @Value("${server.port}")
    private int https_Port;

//    @Bean
//    public FilterRegistrationBean<CorsFilter> corsFilter(){
//        System.out.println("load1");
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        System.out.println("load2");
//        CorsConfiguration config = new CorsConfiguration();
//        System.out.println("load3");
//        config.setAllowCredentials(false);
//        System.out.println("load4");
//        //设置允许的网站域名，*表示任意域名
//        config.addAllowedOrigin("*");
//        System.out.println("load5");
//        //设置请求头信息
//        config.addAllowedHeader("*");
//        System.out.println("load6");
//        //设计允许的请求方法
//        config.addAllowedMethod("*");
//        System.out.println("load7");
//        source.registerCorsConfiguration("/**",config);
//        System.out.println("load8");
//        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
//        System.out.println("load9");
//        bean.setOrder(0);
//        System.out.println("load10");
//        return bean;
//    }

    /**
     * Tomcat配置Https
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
        return tomcat;
    }

    /*
        配置tomcat监听端口
    */
    @Bean
    public Connector initiateHttpConnector(){
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(10001);
        connector.setSecure(true);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(https_Port);
        return connector;

    }

}

